package com.hhy1001.fireplane;

import java.io.IOException;
import java.net.SocketOptions;

import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;

public class MyMusic {
	private MainActivity aMainActivity;
	private MediaPlayer aMediaPlayer;
	private SoundPool aSoundPool;
	public int boom;
	public int boom_minboss;
	public int boom_boss;
	public MyMusic(MainActivity aMainActivity) {
		super();
		this.aMainActivity = aMainActivity;
	}
	
	public void play(String fileName) {
		try {
			if (aMediaPlayer == null) {
				aMediaPlayer = new MediaPlayer();
			}
			aMediaPlayer.reset();
			AssetFileDescriptor assetFileDescriptor;
			assetFileDescriptor = aMainActivity.getAssets().openFd(fileName);
			aMediaPlayer.setDataSource(assetFileDescriptor.getFileDescriptor(),assetFileDescriptor.getStartOffset(),assetFileDescriptor.getLength());
			aMediaPlayer.setLooping(true);
			aMediaPlayer.prepare();
			aMediaPlayer.start();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void  stop() {
		if (aMediaPlayer.isPlaying()) {
			aMediaPlayer.stop();
		}
		
	}
	
	
	public void	 initSoundPool() {
		try {
			aSoundPool = new SoundPool(5,AudioManager.STREAM_MUSIC,0);
			boom = aSoundPool.load(aMainActivity.getAssets().openFd("explosion.mp3"), 1);
			boom_minboss = aSoundPool.load(aMainActivity.getAssets().openFd("explosion2.wav"), 1);
			boom_boss = aSoundPool.load(aMainActivity.getAssets().openFd("bigexplosion.wav"), 1);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void play(int soundID){
		aSoundPool.play(soundID, 1, 1, 1, 0, 1);
	}
}
